using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace TagSystem
{
    public class TagSet
    {
        private Dictionary<string, Tag> _tagDic;
        private Dictionary<string, RegistryTag[]> _position;
        private HashSet<string>[] _registry;
        private Tag[] _tags;

        public TagSet()
        {
            _tagDic = new Dictionary<string, Tag>();
            _tags = new Tag[(int)Tag.ENUM_END];
            _registry = new HashSet<string>[(int)RegistryTag.ENUM_END];
        }

        public void Register(string key, Tag tag, params RegistryTag[] registryTags)
        {
            if (_tagDic.ContainsKey(key)) return;
            _tagDic.Add(key, tag);
            _tags[(int)tag]++;//to deal with the existing tag with a different key
            if (registryTags.Length == 0) return;
            foreach (var item in registryTags) _registry[(int)item].Add(key);
            _position.Add(key, registryTags);
        }

        public void Unregister(string key)
        {
            if (!_tagDic.ContainsKey(key)) return;
            _tagDic.Remove(key);
            _tags[(int)_tagDic[key]]--;
            if (!_position.ContainsKey(key)) return;
            foreach (var item in _position[key]) _registry[(int)item].Remove(key);
            _position.Remove(key);
        }

        public bool TagCheck(Tag tag) { return _tags[(int)tag] != 0; }
    }
}
